package comp6441;

import java.awt.*;
import javax.swing.*;

import java.awt.datatransfer.Clipboard;
import java.awt.datatransfer.ClipboardOwner;
import java.awt.datatransfer.DataFlavor;
import java.awt.datatransfer.StringSelection;
import java.awt.datatransfer.Transferable;
import java.awt.datatransfer.UnsupportedFlavorException;
import java.awt.event.*;
import java.io.*;

//@SuppressWarnings("serial")
public final class Main extends JFrame implements ActionListener, ClipboardOwner
{
	public static void main(String args[])
	{
		new Main().setVisible(true);
	}
	JMenuBar bar;
	JMenu file,basic,additive,macro;
	JMenuItem Newfile,Openfile,Savefile,Exit;
	JMenuItem Copy,Cut,Paste,Delete;
	JMenuItem Copyaddit,Cutaddit;
	JMenuItem keyboard;
	Clipboard clipboard;
	StringSelection clipboardstring;
	Font font;
	JPanel mainpanel;
	JTextArea text;
	
	String command="";
	String Directory="";
	String filename="";
	String Completename="";
	String SelectedText="";
	String Stream="";
	
	int filelength;
	int index;
	
	public Main(){
		super("Empty Note");
		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		setPreferredSize(new Dimension(500, 400));
		pack();
		setLocationRelativeTo(mainpanel);

		JPanel mainpanel=new JPanel(new BorderLayout());
		mainpanel=(JPanel)getContentPane();
		
		bar=new JMenuBar();
		setJMenuBar(bar);
		
		file=new JMenu("File");
		basic=new JMenu("Edit");
		additive=new JMenu("Advanced Editing");
		
		/*
		JMenuItem cutMenu = new JMenuItem(new DefaultEditorKit.CutAction());
		cutMenu.setText("Cut");
		cutMenu.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_X, ActionEvent.CTRL_MASK));

		JMenuItem copyMenu = new JMenuItem(new DefaultEditorKit.CopyAction());
		copyMenu.setText("Copy");
		copyMenu.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_C, ActionEvent.CTRL_MASK));

		JMenuItem pasteMenu = new JMenuItem(new DefaultEditorKit.PasteAction());
		pasteMenu.setText("Paste");
		pasteMenu.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_V, ActionEvent.CTRL_MASK));
		
		macro.add(cutMenu);
		cutMenu.addActionListener(this);
		macro.add(copyMenu);
		copyMenu.addActionListener(this);
		macro.add(pasteMenu);
		pasteMenu.addActionListener(this);*/
		
		file.add(Newfile=new JMenuItem("New file"));
		Newfile.addActionListener(this);
		
		file.add(Openfile=new JMenuItem("Open file"));
		Openfile.addActionListener(this);
		
		file.add(Savefile=new JMenuItem("Save file"));
		Savefile.addActionListener(this);
		
		file.add(Exit=new JMenuItem("Exit"));
		Exit.addActionListener(this);
		
		bar.add(file);
		
		basic.add(Copy=new JMenuItem("Copy"));
		Copy.addActionListener(this);
		
		basic.add(Cut=new JMenuItem ("Cut"));
		Cut.addActionListener(this);
		
		basic.add(Paste=new JMenuItem("Paste"));
		Paste.addActionListener(this);
		
		basic.add(Delete=new JMenuItem("Delete"));
		Delete.addActionListener(this);
		
		bar.add(basic);
		
		additive.add(Copyaddit=new JMenuItem("Additive Copy"));
		Copyaddit.addActionListener(this);
		
		additive.add(Cutaddit=new JMenuItem("Additive Cut"));
		Cutaddit.addActionListener(this);
		
		bar.add(additive);
		
		Macro();
			

		text=new JTextArea();
		mainpanel.add(text,BorderLayout.CENTER);
		setContentPane(mainpanel);

		font=new Font("Arial",Font.PLAIN,13);
		text.setFont(font);
			
	}

public void actionPerformed(ActionEvent action) {
	
	command=(String)action.getActionCommand();
	
	if(command.equalsIgnoreCase("New File"))
	{
	     Newfile();
	}
	else if(command.equalsIgnoreCase("Open File"))
	{
		try {
			
			Openfile();
		}
		catch (IOException e) {}	
	}
	else if(command.equalsIgnoreCase("Save File"))
	{
		try{
		Savefile();
		}
		catch (IOException e){}
	}
	else if(command.equalsIgnoreCase("Exit"))
	{
		Exit();
	}
	else if(command.equalsIgnoreCase("Copy"))
	{
		Copy();	
	}
	else if(command.equalsIgnoreCase("Cut"))
	{
		Cut();	
	}
	else if(command.equalsIgnoreCase("Delete"))
	{
		Delete();	
	}
	else if(command.equalsIgnoreCase("Paste"))
	{
		try {
			Paste();
		} catch (UnsupportedFlavorException e) {
			
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}	
	}
	else if(command.equalsIgnoreCase("Additive Copy"))
	{
		AdditiveCopy();	
	}
	else if(command.equalsIgnoreCase("Additive Cut"))
	{
		AdditiveCut();	
	}
	
}
public void Newfile(){
	dispose();
	Main note = new Main();
	note.setVisible(true);
	
}
public void Openfile() throws IOException{

	String Stream=" ";
	FileDialog dialog=new FileDialog(this,"Open");
	dialog.setVisible(true);

	Directory=dialog.getDirectory();
	filename=dialog.getFile();
	Completename=Directory+filename;
	File file=new File(Completename);
	FileInputStream FileStream=new FileInputStream(file);
	filelength=(int)file.length();
	for(int j=0;j<filelength;j++)
	{
	char str5=(char)FileStream.read();
	Stream=Stream + str5;

	}
	text.setText(Stream);
	
}
public void Savefile() throws IOException{
	
	FileDialog dialog=new FileDialog(this,"Save As",FileDialog.SAVE);
	dialog.setVisible(true);

	Directory=dialog.getDirectory();
	filename=dialog.getFile();
	Completename=Directory+filename;

	Stream=text.getText();
	filelength=Stream.length();
	byte buf[]=Stream.getBytes();

	File file=new File(Completename);
	FileOutputStream FileStream=new FileOutputStream(file);
	for(int counter=0;counter<filelength;counter++)
	{
		FileStream.write(buf[counter]);
	}
    	FileStream.close();
    	this.setTitle(filename);
	}

public void Exit(){
	System.exit(0);
}
public void Copy(){
	
	String SelectedText=text.getSelectedText();
	clipboardstring = new StringSelection(SelectedText);
	Clipboard clipboard = Toolkit.getDefaultToolkit().getSystemClipboard();
	clipboard.setContents(clipboardstring, this);
	
}
public void Cut(){
	
	String SelectedText=text.getSelectedText();
	clipboardstring = new StringSelection(SelectedText);
	Clipboard clipboard = Toolkit.getDefaultToolkit().getSystemClipboard();
	clipboard.setContents(clipboardstring, this);
	
	int startindex=text.getSelectionStart();
	int endindex=text.getSelectionEnd();
	text.replaceRange("",startindex,endindex);	
}
public void Paste() throws UnsupportedFlavorException, IOException{
	
	String Selectedtext="";
	Clipboard clipboard=Toolkit.getDefaultToolkit().getSystemClipboard();
	Transferable clipboarddata=clipboard.getContents(clipboardstring);
    Selectedtext=(String)clipboarddata.getTransferData(DataFlavor.stringFlavor);
	index=text.getCaretPosition();
	text.insert(Selectedtext,index);
}
public void Delete(){
	SelectedText=text.getSelectedText();
	index=text.getText().indexOf(SelectedText);
	text.replaceRange("",index,index+SelectedText.length());
}
public void AdditiveCopy(){
	
	
	String SelectedText=text.getSelectedText();
	String AddSelectedText=SelectedText;
	String additivecopytext=AddSelectedText+SelectedText;
	clipboardstring = new StringSelection(additivecopytext);
	Clipboard clipboard = Toolkit.getDefaultToolkit().getSystemClipboard();
	clipboard.setContents(clipboardstring, this);
	
}
public void AdditiveCut(){
	
	String SelectedText=text.getSelectedText();
	clipboardstring = new StringSelection(SelectedText);
	Clipboard clipboard = Toolkit.getDefaultToolkit().getSystemClipboard();
	clipboard.setContents(clipboardstring, this);
	
	int startindex=text.getSelectionStart();
	int endindex=text.getSelectionEnd();
	text.replaceRange("",startindex,endindex);	
	
}
public void Macro(){
	
	Cut.setAccelerator(KeyStroke.getKeyStroke('U',Event.CTRL_MASK));
	Paste.setAccelerator(KeyStroke.getKeyStroke('P',Event.CTRL_MASK));
	Copy.setAccelerator(KeyStroke.getKeyStroke('Q',Event.CTRL_MASK));
}

@Override
public void lostOwnership(Clipboard clipboard, Transferable contents) {
	// TODO Auto-generated method stub
	
}

}